package weaver.interfaces.suntai.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.interfaces.suntai.utils.InvokeHelper;
import weaver.interfaces.suntai.utils.SunTaiUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-05-12
 * @Description: 销售合同申请流程
 * @ModifyLog:
 **/

public class WeaSalesContractAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int formid = Math.abs(requestInfo.getRequestManager().getFormid());
        User user = requestInfo.getRequestManager().getUser();
        RecordSet rs = new RecordSet();
        int userid = user.getUID();
        writeLog( ">>>>>>WeaSalesContractAction>>>>>" + requestid );
        String resultText = "";
        try {
            rs.executeQuery( "select * from " + tablename + " where requestid=?", requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );
            String sfwbght = Util.null2String( rs.getString( "sfwbght" ) );
            writeLog( ">>>>>>mainid>>>>>" + mainid );
            writeLog( ">>>>>>sfwbght>>>>>" + sfwbght );
            JSONObject jsonData = new JSONObject();
            if ("1".equals( sfwbght )) {//新增销售合同
                jsonData.put( "companyCode", "01-0070-202202-001" );
                jsonData.put( "contractCode", Util.null2String( rs.getString( "htbh1" ) ) );
                jsonData.put( "contractName", Util.null2String( rs.getString( "htmc" ) ) );
                jsonData.put( "contractSecretLev", Util.null2String( rs.getString( "htmj" ) ));
                jsonData.put( "contractAmount", Util.null2String( rs.getString( "htje" ) ) );
                jsonData.put( "currency", Util.null2String( rs.getString( "bzwb" ) ) );
                jsonData.put( "exchangeRate", Util.null2String( rs.getString( "hl" ) ) );
                jsonData.put( "signTime", Util.null2String( rs.getString( "qdsj" ) ) );
                jsonData.put( "startTime", Util.null2String( rs.getString( "kssj" ) ) );
                jsonData.put( "endTime", Util.null2String( rs.getString( "jssj" ) ) );
                jsonData.put( "contractOwner", Util.null2String( rs.getString( "khmcwb" ) ) );
                jsonData.put( "ownerUnifyCode", Util.null2String( rs.getString( "khdm" ) ) );
                jsonData.put( "contractPartyB", Util.null2String( rs.getString( "wfqddwmc" ) ) );
                jsonData.put( "partyBUnifyCode", Util.null2String( rs.getString( "wfqddwdm" ) ) );
                jsonData.put( "contractRemark", Util.null2String( rs.getString( "htbz" ) ) );
                jsonData.put( "fourPlate", Util.null2String( rs.getString( "sdbkwb" ) ) );
                String htlbwb = Util.null2String( rs.getString( "htlbwb" ) );
                jsonData.put( "theirBusiness",htlbwb);
                String jpjfflwb = Util.null2String( rs.getString( "jpjfflwb" ) );
                String htjfflwb = Util.null2String( rs.getString( "htjfflwb" ) );
                jsonData.put( "ownerClassify","JPHT".equals( htlbwb )?jpjfflwb:htjfflwb);
                jsonData.put( "coreBusinessClass", Util.null2String( rs.getString( "hxywflwb" ) ) );
                jsonData.put( "createBy", Util.null2String( rs.getString( "sqrwb" ) ) );
                jsonData.put( "tenantCode", "ZDKST" );
                jsonData.put( "status", "rkz");
                String sfcdzx = Util.null2String( rs.getString( "sfcdzx" ) );
                jsonData.put( "isGreatNewCont", "0".equals( sfcdzx ) ? "Y" : "N" );
                jsonData.put( "isGreatProject", Util.null2String( rs.getString( "sfcdzxwb" )));
                jsonData.put( "armyClass2", "JPHT".equals( htlbwb )?htjfflwb:jpjfflwb);
                jsonData.put( "chargeArmyOffice", Util.null2String( rs.getString( "zgjfjgwb" ) ) );
                jsonData.put( "productType", Util.null2String( rs.getString( "cplxwb" ) ) );
                jsonData.put( "makeType", Util.null2String( rs.getString( "zzlxwb" ) ) );
                jsonData.put( "professionalField", Util.null2String( rs.getString( "zylywb" ) ) );
                jsonData.put( "sciTechClassify", Util.null2String( rs.getString( "kjcxflwb" ) ) );
                jsonData.put( "techEarnings", Util.null2String( rs.getString( "htxzflwb" ) ) );
                jsonData.put( "technicalB", Util.null2String( rs.getString( "jssywb" ) ) );
                jsonData.put( "technicalB2", Util.null2String( rs.getString( "jssyewb" ) ) );
                jsonData.put( "industryClassify", Util.null2String( rs.getString( "xyflwb" ) ) );
                //明细1
                JSONArray progressArray = new JSONArray();
                String fields_dt1 = "sjskdsxhtj,jhskdrq,hskdje";
                rs.executeQuery( "select " + fields_dt1 + " from " + tablename + "_dt1 where mainid=?", mainid );
                while (rs.next()) {
                    JSONObject progressjson = new JSONObject();
                    progressjson.put( "progressItems", Util.null2String( rs.getString( "sjskdsxhtj" ) ) );
                    progressjson.put( "progressTimes", Util.null2String( rs.getString( "jhskdrq" ) ) );
                    progressjson.put( "receivableAmount", Util.null2String( rs.getString( "hskdje" ) ) );
                    progressArray.add( progressjson );
                }
                jsonData.put( "progress", progressArray );

                //明细2
                JSONArray productsArray = new JSONArray();
                rs.executeQuery( "select cpmc,cpxh,cpsl,cpdwwb,htbhgl1 from " + tablename + "_dt2 where mainid=?", mainid );
                while (rs.next()) {
                    JSONObject productsjson = new JSONObject();
                    productsjson.put( "productName", Util.null2String( rs.getString( "cpmc" ) ) );//产品名称
                    String cpxh = Util.null2String( rs.getString( "cpxh" ) );
                    productsjson.put( "productModel", SunTaiUtil.getSelectItem( formid, "cpxh", cpxh ) );//产品型号
                    productsjson.put( "productCount", Util.null2String( rs.getString( "cpsl" ) ) );//产品数量
                    productsjson.put( "productPrice", Util.null2String( rs.getString( "htbhgl1" ) ) );//计量单价
                    productsjson.put( "productUnit", Util.null2String( rs.getString( "cpdwwb" ) ) );//产品单位
                    productsArray.add( productsjson );
                }
                jsonData.put( "products", productsArray );
                writeLog( ">>>>>>jsonData>>>>>" + jsonData.toJSONString() );
                resultText = InvokeHelper.addSellcont( jsonData.toJSONString() );
                writeLog( ">>>>>>resultText>>>>>" + resultText);
                JSONObject resultJson = JSONObject.parseObject( resultText );
                String code = Util.null2String( resultJson.getString( "code" ) );
                String data = Util.null2String( resultJson.getString( "data" ) );
                if (!"200".equals( code )) {
                    requestInfo.getRequestManager().setMessageid( "111111" );
                    requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：" + data );
                    return Action.FAILURE_AND_CONTINUE;
                }
                SunTaiUtil.saveCallInterLog( "销售合同新增接口", resultText, requestid, userid );
            }
            //变更销售合同
            if ("0".equals( sfwbght )) {
                jsonData.put( "companyCode", "01-0070-202202-001" );
                jsonData.put( "contractCode", Util.null2String( rs.getString( "htbhbgwb" ) ) );
                jsonData.put( "changedAmount", Util.null2String( rs.getString( "htje" ) ) );
                jsonData.put( "changedCurrency", Util.null2String( rs.getString( "bzwb" ) ) );
                jsonData.put( "changedExcRate", Util.null2String( rs.getString( "hl" ) ) );
                jsonData.put( "effectiveDate", Util.null2String( rs.getString( "kssj" ) ) );
                jsonData.put( "createBy", Util.null2String( rs.getString( "sqrwb" ) ) );
                jsonData.put( "contractStatus", Util.null2String( rs.getString( "htzt" ) ) );
                jsonData.put( "tenantCode", "ZDKST" );
                jsonData.put( "status", "rkz");
                writeLog( ">>>>>>jsonData>>>>>" + jsonData.toJSONString() );
                resultText = InvokeHelper.addChangeSellcont( jsonData.toJSONString() );
                writeLog( ">>>>>>resultText>>>>>" + resultText);
                JSONObject resultJson = JSONObject.parseObject( resultText );
                String code = Util.null2String( resultJson.getString( "code" ) );
                String message = Util.null2String( resultJson.getString( "message" ) );
                if (!"200".equals( code )) {
                    requestInfo.getRequestManager().setMessageid( "111111" );
                    requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：" + message );
                    return Action.FAILURE_AND_CONTINUE;
                }
                SunTaiUtil.saveCallInterLog( "销售合同变更新增接口", resultText, requestid, userid );
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
